from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
import json
from goods.models import (
    HuaweiCategory,
    HuaweiSubCateGory,
    HuaweiGoods,
    HuaweiGoodsDetialImgList,
    HuaweiGoodContentImageList,
    HuaweiGoodDetialInfos,
)

# import datafiles 1
from huaweiSpider.华为手机系列 import 荣耀畅玩系列 as rycw
from huaweiSpider.华为手机系列 import 移动4G专区 as yd4g
from huaweiSpider.华为手机系列 import HUAWEI麦芒系列 as maimang
from huaweiSpider.华为手机系列 import huaweicx
from huaweiSpider.华为手机系列 import HUAWEIMate系列 as huaweimate
from huaweiSpider.华为手机系列 import HUAWEInova系列 as huaweinova
from huaweiSpider.华为手机系列 import HUAWEIP系列 as huaweiP
from huaweiSpider.华为手机系列 import rongyao

# 5
from huaweiSpider.华为笔记本平板 import 笔记本电脑 as bjbdn
from huaweiSpider.华为笔记本平板 import 笔记本配件 as bjbpj
from huaweiSpider.华为笔记本平板 import 平板电脑 as pbdn

# 4
from huaweiSpider.华为热销配件 import 保护壳 as bhk
from huaweiSpider.华为热销配件 import 保护套 as bht
from huaweiSpider.华为热销配件 import 表带 as bd
from huaweiSpider.华为热销配件 import 充电器线材 as cdxqc
from huaweiSpider.华为热销配件 import 耳机 as ej
from huaweiSpider.华为热销配件 import 摄像机镜头 as sxtjt
from huaweiSpider.华为热销配件 import 生活周边 as shzb
from huaweiSpider.华为热销配件 import 贴膜 as tm
from huaweiSpider.华为热销配件 import 移动电源 as yddy
from huaweiSpider.华为热销配件 import 音箱 as yx
from huaweiSpider.华为热销配件 import 智能眼镜 as znyj
from huaweiSpider.华为热销配件 import 智能硬件 as znyingjian
from huaweiSpider.华为热销配件 import 自拍杆支架 as zpgzj

# 3
from huaweiSpider.华为智能穿戴 import 手表 as shoubiao
from huaweiSpider.华为智能穿戴 import 手环 as shouhuan
from huaweiSpider.华为智能穿戴 import VRs

# 2
from huaweiSpider.华为智能家居智慧屏 import 安防 as anfang
from huaweiSpider.华为智能家居智慧屏 import 厨电 as chudian
from huaweiSpider.华为智能家居智慧屏 import 电力猫wifi放大器 as dianliwifi
from huaweiSpider.华为智能家居智慧屏 import 电视盒子 as dianshihezi
from huaweiSpider.华为智能家居智慧屏 import 环境 as huanjing
from huaweiSpider.华为智能家居智慧屏 import 健康 as jiankang
from huaweiSpider.华为智能家居智慧屏 import 节能 as jieneng
from huaweiSpider.华为智能家居智慧屏 import 路由器 as luyouqi
from huaweiSpider.华为智能家居智慧屏 import 其他 as others
from huaweiSpider.华为智能家居智慧屏 import 清洁 as qingjie
from huaweiSpider.华为智能家居智慧屏 import 卫浴 as weiyu
from huaweiSpider.华为智能家居智慧屏 import 移动路由 as yidongluyou
from huaweiSpider.华为智能家居智慧屏 import 影音 as yinying
from huaweiSpider.华为智能家居智慧屏 import 运动 as yundong
from huaweiSpider.华为智能家居智慧屏 import 照明 as zhaoming
from huaweiSpider.华为智能家居智慧屏 import 智慧屏 as zhihuipin
from huaweiSpider.华为智能家居智慧屏 import 子母分布式路由 as zmfbsly

# 添加视图视图开始************************************************
# 访问添加主目录
def addCategory(request):
    if request.method == "GET":
        data = [
            {"categoryName": "华为手机系列"},
            {"categoryName": "华为智能家居 & 智慧屏"},
            {"categoryName": "华为智能穿戴"},
            {"categoryName": "华为热销配件"},
            {"categoryName": "华为笔记本&平板"},
        ]

        for item in data:
            cate = HuaweiCategory()
            cate.categoryName = item["categoryName"]
            cate.save()

        return HttpResponse("添加成功!")


# 访问添加二级目录以及商品相关数据
def addChildCategory(request, pk):
    if request.method == "GET":
        if False:
            return HttpResponse("已有数据不可添加!")
        else:
            if pk == 5:
                parentcate = HuaweiCategory.objects.get(pk=pk)
                data = [
                    {"subCategoryName": "笔记本电脑"},
                    {"subCategoryName": "笔记本配件"},
                    {"subCategoryName": "平板电脑"},
                ]
                for item in data:
                    subCate = HuaweiSubCateGory()
                    subCate.subCategoryName = item["subCategoryName"]
                    subCate.parentCategory = parentcate
                    subCate.save()
                    if item["subCategoryName"] == "笔记本电脑":
                        finalData = bjbdn.data
                    elif item["subCategoryName"] == "笔记本配件":
                        finalData = bjbpj.data
                    elif item["subCategoryName"] == "平板电脑":
                        finalData = pbdn.data
                    addgooddata(finalData, subCate, parentcate)
                return HttpResponse("添加笔记本&平板数据成功！")
            elif pk == 4:
                parentcate = HuaweiCategory.objects.get(pk=pk)
                data = [
                    {"subCategoryName": "保护壳"},
                    {"subCategoryName": "保护套"},
                    {"subCategoryName": "表带"},
                    {"subCategoryName": "充电器线材"},
                    {"subCategoryName": "耳机"},
                    {"subCategoryName": "摄像头镜头"},
                    {"subCategoryName": "生活周边"},
                    {"subCategoryName": "贴膜"},
                    {"subCategoryName": "移动电源"},
                    {"subCategoryName": "音箱"},
                    {"subCategoryName": "智能眼镜"},
                    {"subCategoryName": "智能硬件"},
                    {"subCategoryName": "自拍杆支架"},
                ]
                for item in data:
                    subCate = HuaweiSubCateGory()
                    subCate.subCategoryName = item["subCategoryName"]
                    subCate.parentCategory = parentcate
                    subCate.save()
                    if item["subCategoryName"] == "保护壳":
                        finalData = bhk.data
                    elif item["subCategoryName"] == "保护套":
                        finalData = bht.data
                    elif item["subCategoryName"] == "表带":
                        finalData = bd.data
                    elif item["subCategoryName"] == "充电器线材":
                        finalData = cdxqc.data
                    elif item["subCategoryName"] == "耳机":
                        finalData = ej.data
                    elif item["subCategoryName"] == "摄像头镜头":
                        finalData = sxtjt.data
                    elif item["subCategoryName"] == "生活周边":
                        finalData = shzb.data
                    elif item["subCategoryName"] == "贴膜":
                        finalData = tm.data
                    elif item["subCategoryName"] == "移动电源":
                        finalData = yddy.data
                    elif item["subCategoryName"] == "音箱":
                        finalData = yx.data
                    elif item["subCategoryName"] == "智能眼镜":
                        finalData = znyj.data
                    elif item["subCategoryName"] == "智能硬件":
                        finalData = znyingjian.data
                    elif item["subCategoryName"] == "自拍杆支架":
                        finalData = zpgzj.data
                    addgooddata(finalData, subCate, parentcate)
                return HttpResponse("添加热销配件数据成功！")
            elif pk == 1:
                parentcate = HuaweiCategory.objects.get(pk=pk)
                data = [
                    {"subCategoryName": "华为畅享系列"},
                    {"subCategoryName": "荣耀畅玩系列"},
                    {"subCategoryName": "移动4G专区"},
                    {"subCategoryName": "HUAWEI麦芒系列"},
                    {"subCategoryName": "HUAWEIMate系列"},
                    {"subCategoryName": "HUAWEInova系列"},
                    {"subCategoryName": "HUAWEIP系列"},
                    {"subCategoryName": "华为荣耀系列"},
                ]
                for item in data:
                    subCate = HuaweiSubCateGory()  # 实例化二级目录
                    subCate.subCategoryName = item["subCategoryName"]
                    subCate.parentCategory = parentcate
                    subCate.save()
                    if item["subCategoryName"] == "荣耀畅玩系列":
                        finalData = rycw.data
                    elif item["subCategoryName"] == "华为畅享系列":
                        finalData = huaweicx.data
                    elif item["subCategoryName"] == "移动4G专区":
                        finalData = yd4g.data
                    elif item["subCategoryName"] == "HUAWEI麦芒系列":
                        finalData = maimang.data
                    elif item["subCategoryName"] == "HUAWEIMate系列":
                        finalData = huaweimate.data
                    elif item["subCategoryName"] == "HUAWEInova系列":
                        finalData = huaweinova.data
                    elif item["subCategoryName"] == "HUAWEIP系列":
                        finalData = huaweiP.data
                    elif item["subCategoryName"] == "华为荣耀系列":
                        finalData = rongyao.data
                    addgooddata(finalData, subCate, parentcate)
                    # for items in finalData:
                    #     good = HuaweiGoods()  # 实例化商品模型
                    #     good.goodName = items["phoneName"]
                    #     good.goodCoverImg = items["phoneCoverImg"]
                    #     good.goodPrice = items["phonePrice"].replace("￥","")
                    #     good.goodDetailNmae = items["phoneDetailNmae"]
                    #     good.goodCategory = subCate
                    #     good.save()
                    #     for itemss in items["detialImagesList"]:
                    #         detialImagesList = HuaweiGoodsDetialImgList()
                    #         detialImagesList.images = itemss.replace(
                    #             "78_78", "800_800", 1
                    #         )
                    #         detialImagesList.imagessmall = itemss
                    #         detialImagesList.goods = good
                    #         detialImagesList.save()
                    #     for itemss in items["contentImagesList"]:
                    #         contentImagesList = HuaweiGoodContentImageList()
                    #         contentImagesList.images = itemss
                    #         contentImagesList.goods = good
                    #         contentImagesList.save()
                    #     for itemss in items["phoneInfos"]:
                    #         phoneInfos = HuaweiGoodDetialInfos()
                    #         phoneInfos.detailName = itemss["provideName"]
                    #         phoneInfos.detialValue = itemss["provideNameVal"]
                    #         phoneInfos.goods = good
                    #         phoneInfos.save()

                return HttpResponse("添加手机数据成功！")

            elif pk == 3:
                parentcate = HuaweiCategory.objects.get(pk=pk)
                data = [
                    {"subCategoryName": "手表"},
                    {"subCategoryName": "手环"},
                    {"subCategoryName": "VR"},
                ]
                for item in data:
                    subCate = HuaweiSubCateGory()
                    subCate.subCategoryName = item["subCategoryName"]
                    subCate.parentCategory = parentcate
                    subCate.save()
                    if item["subCategoryName"] == "手表":
                        finalData = shoubiao.data
                    elif item["subCategoryName"] == "手环":
                        finalData = shouhuan.data
                    elif item["subCategoryName"] == "VR":
                        finalData = VRs.data
                    addgooddata(finalData, subCate, parentcate)
                return HttpResponse("添加智能穿戴数据成功！")
            elif pk == 2:
                parentcate = HuaweiCategory.objects.get(pk=pk)
                data = [
                    {"subCategoryName": "安防"},
                    {"subCategoryName": "厨电"},
                    {"subCategoryName": "电力猫wifi放大器"},
                    {"subCategoryName": "电视盒子"},
                    {"subCategoryName": "环境"},
                    {"subCategoryName": "健康"},
                    {"subCategoryName": "节能"},
                    {"subCategoryName": "路由器"},
                    {"subCategoryName": "其他"},
                    {"subCategoryName": "清洁"},
                    {"subCategoryName": "卫浴"},
                    {"subCategoryName": "移动路由"},
                    {"subCategoryName": "影音"},
                    {"subCategoryName": "运动"},
                    {"subCategoryName": "照明"},
                    {"subCategoryName": "智慧屏"},
                    {"subCategoryName": "子母分布式路由"},
                ]
                for item in data:
                    subCate = HuaweiSubCateGory()
                    subCate.subCategoryName = item["subCategoryName"]
                    subCate.parentCategory = parentcate
                    subCate.save()
                    if item["subCategoryName"] == "安防":
                        finalData = anfang.data
                    elif item["subCategoryName"] == "厨电":
                        finalData = chudian.data
                    elif item["subCategoryName"] == "电力猫wifi放大器":
                        finalData = dianliwifi.data
                    elif item["subCategoryName"] == "电视盒子":
                        finalData = dianshihezi.data
                    elif item["subCategoryName"] == "环境":
                        finalData = huanjing.data
                    elif item["subCategoryName"] == "健康":
                        finalData = jiankang.data
                    elif item["subCategoryName"] == "节能":
                        finalData = jieneng.data
                    elif item["subCategoryName"] == "路由器":
                        finalData = luyouqi.data
                    elif item["subCategoryName"] == "其他":
                        finalData = others.data
                    elif item["subCategoryName"] == "清洁":
                        finalData = qingjie.data
                    elif item["subCategoryName"] == "卫浴":
                        finalData = weiyu.data
                    elif item["subCategoryName"] == "移动路由":
                        finalData = yidongluyou.data
                    elif item["subCategoryName"] == "影音":
                        finalData = yinying.data
                    elif item["subCategoryName"] == "运动":
                        finalData = yundong.data
                    elif item["subCategoryName"] == "照明":
                        finalData = zhaoming.data
                    elif item["subCategoryName"] == "智慧屏":
                        finalData = zhihuipin.data
                    elif item["subCategoryName"] == "子母分布式路由":
                        finalData = zmfbsly.data
                    addgooddata(finalData, subCate, parentcate)
                return HttpResponse("添加只能家居智慧屏数据成功！")


def addgooddata(data, subCate, perantcate):
    for items in data:
        good = HuaweiGoods()  # 实例化商品模型
        good.goodName = items["phoneName"]
        good.goodCoverImg = items["phoneCoverImg"]
        good.goodPrice = float(items["phonePrice"].replace("¥", ""))
        good.goodDetailNmae = items["phoneDetailNmae"]
        good.goodCategory = subCate
        good.mainCategory = perantcate
        good.save()
        for itemss in items["detialImagesList"]:
            detialImagesList = HuaweiGoodsDetialImgList()
            detialImagesList.images = itemss.replace("78_78", "800_800", 1)
            detialImagesList.imagessmall = itemss
            detialImagesList.goods = good
            detialImagesList.save()
        for itemss in items["contentImagesList"]:
            contentImagesList = HuaweiGoodContentImageList()
            contentImagesList.images = itemss
            contentImagesList.goods = good
            contentImagesList.save()
        for itemss in items["phoneInfos"]:
            phoneInfos = HuaweiGoodDetialInfos()
            phoneInfos.detailName = itemss["provideName"]
            phoneInfos.detialValue = itemss["provideNameVal"]
            phoneInfos.goods = good
            phoneInfos.save()


# 添加数据视图结束************************************************


# 商品视图部分*****************************************************
from rest_framework import viewsets
from goods.serializers import (
    HuaweiCategorySerializer,
    GoodClickNumSerializer,
    HuaweiSubCateGorySerializer,
    HuaweiGoodsSerializer,
    HuaweiGoodContentImageListSerializer,
    HuaweiGoodsDetialImgListSerializer,
    HuaweiGoodDetialInfosSerializer,
)
from rest_framework.filters import OrderingFilter, SearchFilter
from rest_framework import permissions, mixins
from rest_framework.pagination import LimitOffsetPagination

# 配置djangorestframwork自带的缓存
# from rest_framework_extensions.cache.mixins import CacheResponseMixin

# 总目录
class HuaweiCategoryViewset(
    viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin
):
    queryset = HuaweiCategory.objects.all()
    serializer_class = HuaweiCategorySerializer
    permission_classes = [permissions.AllowAny]


# 二级目录
class HuaweiSubCateGoryViewset(
    viewsets.GenericViewSet,
    mixins.ListModelMixin,
    mixins.RetrieveModelMixin,
):
    serializer_class = HuaweiSubCateGorySerializer
    permission_classes = [permissions.AllowAny]
    pagination_class = LimitOffsetPagination  # 前段自定义每页显示数量?limit=n

    def get_queryset(self):
        parentCategory = self.request.query_params.get("parentcategory", None)
        if parentCategory is not None:
            queryset = HuaweiSubCateGory.objects.filter(parentCategory=parentCategory)
        else:
            queryset = HuaweiSubCateGory.objects.all()
        return queryset


# 产品
class HuaweiGoodsViewset(
    viewsets.GenericViewSet,
    mixins.ListModelMixin,
    mixins.RetrieveModelMixin,
    mixins.UpdateModelMixin,
):
    # serializer_class = HuaweiGoodsSerializer
    permission_classes = [permissions.AllowAny]
    filter_backends = [OrderingFilter, SearchFilter]
    ordering_fields = ["goodPrice", "goodNums", "goodSoldNums", "goodFavNums"]
    search_fields = ["goodName", "goodDetailNmae"]  # 目前还不能搜索到外键数据
    ordering = ["pk"]  #  默认排序项
    pagination_class = LimitOffsetPagination  # 前段自定义每页显示数量?limit=n
    # 自定义queryset的值---按照前端传过来的类别的值来确定数据范围
    def get_queryset(self):  # 前端传的是类别的id
        caetgory = self.request.query_params.get("category", None)
        mainCategory = self.request.query_params.get("mainCategory", None)
        if caetgory is not None:
            # filter(条件1,条件2,...)----可以从前端传更多参数来缩小查询集
            queryset = HuaweiGoods.objects.filter(goodCategory=caetgory)
        elif mainCategory is not None:
            queryset = HuaweiGoods.objects.filter(mainCategory=mainCategory)
        else:
            queryset = HuaweiGoods.objects.all()
        return queryset

    def get_serializer_class(self):
        serializer_class = HuaweiGoodsSerializer
        if self.action == "update":
            serializer_class = GoodClickNumSerializer

        return serializer_class


# 产品详情页内容图
class HuaweiGoodContentImageListViewset(
    viewsets.GenericViewSet,
    mixins.ListModelMixin,
    mixins.RetrieveModelMixin,
):
    serializer_class = HuaweiGoodContentImageListSerializer
    permission_classes = [permissions.AllowAny]
    pagination_class = LimitOffsetPagination  # 前段自定义每页显示数量?limit=n

    def get_queryset(self):
        goods = self.request.query_params.get("goods", None)
        if goods is not None:
            queryset = HuaweiGoodContentImageList.objects.filter(goods=goods)
        else:
            queryset = HuaweiGoodContentImageList.objects.all()
        return queryset


# 产品详细图
class HuaweiGoodsDetialImgListViewset(
     viewsets.GenericViewSet, mixins.ListModelMixin
):
    serializer_class = HuaweiGoodsDetialImgListSerializer
    pagination_class = LimitOffsetPagination  # 前段自定义每页显示数量?limit=n

    def get_queryset(self):
        goods = self.request.query_params.get("goods", None)
        if goods is not None:
            queryset = HuaweiGoodsDetialImgList.objects.filter(goods=goods)
        else:
            queryset = HuaweiGoodsDetialImgList.objects.all().order_by("pk")
        return queryset


# 产品参数


class HuaweiGoodDetialInfosViewset(
    viewsets.GenericViewSet,
    mixins.ListModelMixin,
    mixins.RetrieveModelMixin,
):
    serializer_class = HuaweiGoodDetialInfosSerializer
    pagination_class = LimitOffsetPagination  # 前段自定义每页显示数量?limit=n

    def get_queryset(self):
        goods = self.request.query_params.get("goods", None)
        if goods is not None:
            queryset = HuaweiGoodDetialInfos.objects.filter(goods=goods)
        else:
            queryset = HuaweiGoodDetialInfos.objects.all()
        return queryset
